﻿using ERP.ERPSystem.WarehouseManagement.Dto;
using ERP.Team.WarehouseManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ERP.ERPSystem.WarehouseManagement
{
    /// <summary>
    /// 仓储管理-销售退货-xzc
    /// </summary>
    public class SalesReturnServices:ApplicationService, ISalesReturnServices
    {
        private readonly IRepository<SalesReturns> _salesreturnsRepositroy;

        public SalesReturnServices(IRepository<SalesReturns> salesreturnsRepositroy)
        {
            _salesreturnsRepositroy = salesreturnsRepositroy;
        }
        /// <summary>
        /// 新增销售退货
        /// </summary>
        /// <param name="salesrtrunsdto"></param>
        /// <returns></returns>
        public async Task<int> AddSalesReturns(SalesReturnsDto salesrtrunsdto)
        {
            var i = ObjectMapper.Map<SalesReturnsDto, SalesReturns>(salesrtrunsdto);
            var salesreturnsList = await _salesreturnsRepositroy.InsertAsync(i);
            return salesreturnsList == null ? 0 : 1;
        }
        /// <summary>
        /// 更新销售退货
        /// </summary>
        /// <param name="salesrtrunsdto"></param>
        /// <returns></returns>
        public async Task<int> UpdateSalesReturns(SalesReturnsDto salesrtrunsdto)
        {
            var i = ObjectMapper.Map<SalesReturnsDto, SalesReturns>(salesrtrunsdto);
            var salesreturnsList = await _salesreturnsRepositroy.UpdateAsync(i);
            return salesreturnsList == null ? 0 : 1;
        }
        /// <summary>
        /// 删除销售退货
        /// </summary>
        /// <param name="salesrtrunsdto"></param>
        /// <returns></returns>
        public async Task DeleteSalesReturns(int id)
        {
            await _salesreturnsRepositroy.DeleteAsync(m=>m.Id == id);
        }
        /// <summary>
        /// 查询销售退货
        /// </summary>
        /// <param name="getissuingmaterialsdto"></param>
        /// <returns></returns>
        public async Task<List<SalesReturns>> GetSalesReturns(GetSalesReturnsDto getalesReturndto)
        {
            var issuingmaterialsList = await _salesreturnsRepositroy.GetListAsync();
            issuingmaterialsList = issuingmaterialsList.WhereIf(!string.IsNullOrEmpty(getalesReturndto.ReturnNumber), m => m.ReturnNumber == getalesReturndto.ReturnNumber).ToList();
            issuingmaterialsList = issuingmaterialsList.WhereIf(!string.IsNullOrEmpty(getalesReturndto.SalesOrdersCode), m => m.SalesOrdersCode == getalesReturndto.SalesOrdersCode).ToList();
            issuingmaterialsList = issuingmaterialsList.WhereIf(!string.IsNullOrEmpty(getalesReturndto.CustomeName), m => m.CustomeName.Contains(getalesReturndto.CustomeName)).ToList();
            issuingmaterialsList = issuingmaterialsList.WhereIf(getalesReturndto.DocumentStatus > 0, m => m.DocumentStatus == getalesReturndto.DocumentStatus).ToList();
            return issuingmaterialsList;

        }
        /// <summary>
        /// 通过主键id查询到相对应销售退货
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<SalesReturns>> FindSalesReturns(int id)
        {
            var issuingmaterialsList = await _salesreturnsRepositroy.GetListAsync(m=>m.Id == id);
            return issuingmaterialsList;

        }
        /// <summary>
        /// 批量删除销售退货
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> BatchDeleteSalesReturns(List<int> ids)
        {
            if (ids == null || ids.Count == 0)
            {
                return 0;
            }

            try
            {
                await _salesreturnsRepositroy.DeleteAsync(m => ids.Contains(m.Id));
                return ids.Count;
            }
            catch
            {
                return 0;
            }
        }
    }
}
